#include "GameCC.h"

Category::Category(char* Name)
{
   strcpy(strName, Name);
}

void Category::add(char* question)
{
	Questions.add(question);
}

int Category::find(char* answer)
{
   for (int i = 0; i < getCount(); i++)
   {
      if (stricmp(answer, Questions.item(i)) == 0)
         return i;
   }
   return -1;
}

void Category::load(void)
{
	char buf[MAX_BUFFER];
	char *name;
	
	GetPrivateProfileString("Categories", strName, "", buf, sizeof(buf), GetIniFileName());
	
	clear();
	
	name = strtok(buf, DELIMITERS);
	while(name)
	{
		add(name);
		name = strtok(NULL, DELIMITERS);
	}		
}

void CategoriesControlCenter::add(char* Name)
{
	Category* NewCategory; 
   NewCategory = new Category(Name);
	NewCategory->load();
	Categories.add((void*)NewCategory);
}

Category* CategoriesControlCenter::getRandom(void)
{
   return (Category*)Categories.getRandomItem();
}

void CategoriesControlCenter::load(void)
{
   char buf[MAX_BUFFER];
	char *name;

   clear();
   GetPrivateProfileSection("Categories", buf, sizeof(buf), GetIniFileName());

   for (char* pName = buf; pName && *pName; pName = NextToken(pName, '\0'))
   {
		char entry[MAX_BUFFER];

		strcpy(entry, pName);
		name = strtok(entry, DELIMITERS);
      add(name);
   }
}

void CategoriesControlCenter::print(bool isNumbered)
{
   ClearScreen();
   if (isNumbered)
     puts("0 - All Categories");
	for (int i = 0; i < Categories.getCount(); i++)
	{
		Category* category = (Category*) Categories.item(i);
      if (isNumbered)
         printf("%d - %s\n", i+1, category->strName);
      else
		   puts(category->strName);
	}
   if (!isNumbered)
      WaitForKey(PRESS_ANY_KEY);
}
